<?php

namespace App\Http\Controllers\Personal;

use App\Model\Appeal;
use App\Model\appeal_desc;
use App\Model\Sch_desc;
use App\Model\Staff;
use App\Model\Store;
use App\Model\Users;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
//今日签到
class SignController extends CommonController
{
    //
	protected $users;
	protected $userData;
	protected $Sch_desc;
	protected $Appeal;
	protected $appeal_desc;
	protected $Staff;
	const PI = 3.1415926;

	public function __construct()
	{
		$this->userData=Auth::user();
		parent::__construct();
		$this->users=new Users();
		$this->Sch_desc=new Sch_desc();
		$this->Store=new Store();
		$this->Appeal=new Appeal();
		$this->appeal_desc=new appeal_desc();
		$this->Staff=new Staff();
	}


//	获取该用户所有的任务
	public function all_task(){
		$uid=$this->userData->id;
		$data=$this->users->alltask($uid);
		foreach($data['staffs'] as $kk=>$vv){
			$newArr[$kk]['name']=$vv['staff_task']['store_station']['name'];
			$newArr[$kk]['id']=$vv['staff_task']['task_id'];
			$newArr[$kk]['staff_id']=$vv['staff_task']['staff_id'];
		}
		return $this->success($newArr) ;
	}



//	开始签到
	public function sign_start(Request $request){
		$aa=$this->confine('121.564128','31.205986','121.564128','31.204950',10);
		if($aa){
			return 1;
		}else{
			return 2;
		}
		$staff_id=$request->staff_id;
		$time=$request->sign_start;
//		获取应该签到的记录
		$signData=$this->Sch_desc->where(['staff_id'=>$staff_id,'time'=>date("Y-m-d",strtotime($time))])->first();
		$storeData=$this->Store->where(['id'=>$signData['store_id']])->first();
		$longitude=$storeData['longitude'];//121.564128
		$latitude=$storeData['latitudef'];//31.205986
//		判断当前签到时间
		if($time<$signData['time'].$signData['start_time']){
			$data['message']='迟到';$data['status']=1;
		}else{
			$data['message']='正常';$data['status']=0;
		}
//		判断是否在签到范围内

		DB::beginTransaction();
		try{
			$result=$this->Sch_desc->where(['id'=>$signData['id']])->update(['sign_start'=>date("H:i",strtotime($time))]);
			DB::commit();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->updated($data);
		}else{
			return $this->failed($message);
		}
	}

//	获取签到时间
	public function sign_time(Request $request){
		$staff_id=$request->staff_id;
		$data=$this->Sch_desc->where(['staff_id'=>$staff_id])->first();
		if(count($data)){
			$result['start_time']=$data['start_time'];
			$result['end_time']=$data['end_time'];
			return $this->success($result) ;
		}else{
			return $this->failed('无法获取');
		}
	}


//	结束签到
	public function sign_end(Request $request){
		$staff_id=$request->staff_id;
		$time=$request->sign_end;
//		获取应该签到的记录
		$signData=$this->Sch_desc->where(['staff_id'=>$staff_id,'time'=>date("Y-m-d",strtotime($time))])->first();
		$storeData=$this->Store->where(['id'=>$signData['store_id']])->first();
		$longitude=$storeData['longitude'];
		$latitude=$storeData['latitudef'];
//		判断当前签到时间
		if($time<$signData['time'].$signData['end_time']){
			$data['message']='早退';$data['status']=1;
		}else{
			$data['message']='正常';$data['status']=0;
		}
		DB::beginTransaction();
		try{
			$result=$this->Sch_desc->where(['id'=>$signData['id']])->update(['sign_end'=>date("H:i",strtotime($time))]);
			DB::commit();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->updated($data);
		}else{
			return $this->failed($message);
		}
	}


//  申诉列表接口
	public function appeal_list(Request $request){
		$data=$this->Appeal->get();
		return $this->success($data);
	}


//	提交申诉
	public function add_appeal(Request $request){
		try{
			$data=$request->input();
			$data['store_id']=$this->Staff->staff_store($data['staff_id'])->store->id;
			$result=$this->appeal_desc->create($data);
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->stored($result);
		}else{
			return $this->failed($message);
		}
	}













//	---------------------------------------------判断距离是否在范围内---------------------------
	/**
	 * 判断是否在有效范围内
	 *
	 * Date: 06/05/2018
	 * @author George
	 * @param float $sta_lon
	 * @param float $sta_lat
	 * @param float $des_lon
	 * @param float $des_lat
	 * @param int $radius
	 * @return bool
	 */
	public static function confine( $sta_lon,  $sta_lat,  $des_lon,  $des_lat,  $radius)
	{
		$distance = self::distance($sta_lon, $sta_lat, $des_lon, $des_lat);
		return $distance - $radius < 0;
	}

	/**
	 * 计算两点间距离
	 *
	 * Date: 06/05/2018
	 * @author George
	 * @param float $sta_lon 起始点经度
	 * @param float $sta_lat 起始点纬度
	 * @param float $des_lon 目的地经度
	 * @param float $des_lat 目的地纬度
	 * @param string $unit 单位，默认是米
	 * @param int $decimal 计算精度
	 * @return float
	 */
	public static function distance( $sta_lon,  $sta_lat,  $des_lon,  $des_lat,  $unit = 'm',  $decimal = 2)
	{
		$radius = 6370.996;

		$sta_lon = ($sta_lon * self::PI) / 180;
		$sta_lat = ($sta_lat * self::PI) / 180;
		$des_lon = ($des_lon * self::PI) / 180;
		$des_lat = ($des_lat * self::PI) / 180;

		$a = $sta_lat - $des_lat;
		$b = $sta_lon - $des_lon;

		$distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($sta_lat) * cos($des_lat) * pow(sin($b / 2), 2)));
		$distance = $distance * $radius * 1000;

		if ($unit === 'km') {
			$distance = $distance / 1000;
		}

		return round($distance, $decimal);
	}
}
